package com.onpositive.datamap.values.impl;

import java.text.NumberFormat;

import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.IScalarRange;
import com.onpositive.datamap.core.IDataProperty;

public class ScalarRange extends ScalarValue implements IScalarRange<Double> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7628191386068351508L;

	public static final ScalarRange NONE = new ScalarRange(null, null, -Double.MAX_VALUE/2);

	protected double lowValue;
	protected double highValue;

	public double getLowValue() {
		return lowValue;
	}

	public double getHighValue() {
		return highValue;
	}

	protected ScalarRange(){
		
	}
	
	public ScalarRange(IDataProperty prop, Object comment, double low, double high) {
		super(prop, comment);
		this.lowValue = low;
		this.highValue = high;
	}

	public ScalarRange(IDataProperty prop, Object comment, double low) {
		super(prop, comment);
		this.lowValue = low;
		this.highValue = low;
	}

	@Override
	public int compareTo(IScalarRange<Double> o) {
		double high2 = this.getHigh();
		if (o==null){
			return 1;
		}
		double olow = o.getHigh();
		if (high2 > olow) {
			return 1;
		}
		if (high2 < olow) {
			return -1;
		}
		return (this.getLow().compareTo(o.getLow()));

	}

	@Override
	public Double getLow() {
		return lowValue;
	}

	@Override
	public Double getHigh() {
		return highValue;
	}

	@Override
	public String toString() {
		if (isNone()){
			return "None";
		}
		if (isVarious()){
			return "Various";
		}
		
		if (lowValue == highValue) {
			
			return NumberFormat.getInstance().format(lowValue);
		}
		return NumberFormat.getInstance().format(lowValue) + "-"
				+ NumberFormat.getInstance().format(highValue);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(highValue);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(lowValue);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ScalarRange other = (ScalarRange) obj;
		if (!this.toString().equals(other.toString())){
			return false;
		}
		return true;
	}

	public final boolean isNone() {
		return lowValue==-Double.MAX_VALUE/2;
	}

	public final boolean isVarious() {
		return lowValue==-Double.MAX_VALUE/3;
	}
	@Override
	public IDataPropertyValue clone(AbstractValueAccessor accessor) {
		return new ScalarRange(prop.clone(accessor),getComment(),lowValue,highValue);
	}
}
